package com.mingyuan.library.infra.common.event.publisher;

import com.mingyuan.library.domain.common.event.DomainEvent;
import com.mingyuan.library.domain.common.event.DomainEvents;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.AllArgsConstructor;

/**
 * 计量领域事件发布者
 */
@AllArgsConstructor
public class MeteredDomainEventPublisher implements DomainEvents {

    private static final String DOMAIN_EVENTS = "domain_events";
    private static final String TAG_NAME = "name";
    /**
     * 领域事件
     */
    private final DomainEvents delegate;
    private final MeterRegistry metricsRegistry;

    /**
     * 发布事件
     * @author yangzk
     * @param event 领域事件信息
     * @date 2024/1/5 14:25
     **/
    @Override
    public void publish(DomainEvent event) {

        delegate.publish(event);
        metricsRegistry.counter(DOMAIN_EVENTS, TAG_NAME, event.getClass().getSimpleName()).increment();
    }
}
